package com.ujs.educms.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ujs.commonutils.constant.R;
import com.ujs.educms.entity.CrmBanner;
import com.ujs.educms.entity.vo.BannerVo;
import com.ujs.educms.service.CrmBannerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 首页banner表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2023-08-10
 */
@RestController
@RequestMapping("/educms/banneradmin")
@Api(tags = "后台banner管理")
public class BannerAdminController {
    @Autowired
    private CrmBannerService bannerService;

    // 1.分页查询banner
    @GetMapping("/pageBanner/{current}/{limit}")
    @ApiOperation("分页查询banner")
    public R pageBanner(@ApiParam("当前页") @PathVariable("current") Long page,
                        @ApiParam("每页记录数") @PathVariable("limit") Long limit) {
        Page<CrmBanner> bannerPage = new Page<>(page, limit);
        IPage<CrmBanner> iPage = bannerService.page(bannerPage, null);
        return R.ok().data("items", iPage.getRecords()).data("total", iPage.getTotal());
    }

    @DeleteMapping("/deleteBanner/{bannerId}")
    @ApiOperation("删除banner")
    public R deleteBannerById(@PathVariable("bannerId") String bannerId) {
        bannerService.removeById(bannerId);
        return R.ok();
    }

    @PostMapping("/addBanner")
    @ApiOperation("添加banner")
    public R addBanner(@RequestBody BannerVo bannerVo) {
        CrmBanner banner = new CrmBanner();
        BeanUtils.copyProperties(bannerVo, banner);
        bannerService.save(banner);
        return R.ok();
    }

    @PostMapping("/updateBanner")
    @ApiOperation("修改banner")
    public R updateBanner(@RequestBody CrmBanner banner) {
        bannerService.updateById(banner);
        return R.ok();
    }

    @GetMapping("/getBanner/{bannerId}")
    @ApiOperation("通过id查询banner")
    public R getBanner(@PathVariable("bannerId") String bannerId) {
        CrmBanner banner = bannerService.getById(bannerId);
        return R.ok().data("banner", banner);
    }
}

